package cn.relx.org.controller;

import cn.relx.basic.util.AjaxResult;
import cn.relx.basic.util.PageList;
import cn.relx.org.domain.SystemDictionaryDetail;
import cn.relx.org.domain.SystemDictionaryType;
import cn.relx.org.query.DepartmentQuery;
import cn.relx.org.service.SystemDictionaryDetailService;
import cn.relx.org.service.SystemDictionaryTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/DictionaryType")
public class SystemDictionaryTypeController {
    @Autowired
    private SystemDictionaryTypeService typeService;

    @Autowired
    private SystemDictionaryDetailService detailService;

    @GetMapping
    public List<SystemDictionaryType> findAll(){
        return typeService.findAll();
    }

    @PostMapping
    public PageList<SystemDictionaryType> queryByPage(@RequestBody DepartmentQuery query){
        return typeService.queryByPage(query);
    }

    @DeleteMapping("/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        Long typesId = typeService.findByTeptsId(id);
        if(typesId != 0){
            detailService.delTypesId(id);
            typeService.delete(id);
        }
        typeService.delete(id);
        return AjaxResult.me().success();
    }

    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody SystemDictionaryType sysType){
        if(sysType.getId() == null){
            typeService.add(sysType);
        }else {
            typeService.update(sysType);
        }
        return AjaxResult.me().success();
    }

    @GetMapping("/{id}")
    public SystemDictionaryType findById(@PathVariable("id") Long id){
        return typeService.findById(id);
    }

    @PatchMapping
    public AjaxResult batchDel(@RequestBody List<Long> ids){
        typeService.batchDel(ids);
        return AjaxResult.me().success();
    }





    @GetMapping("/drawer/{id}")
    public List<SystemDictionaryDetail> findDrawer(@PathVariable("id") Long id){
        System.out.println(typeService.findDrawer(id));
        List<SystemDictionaryDetail> detail = typeService.findDrawer(id);
        return detail;
    }
}
